package com.example.testapplication.ui.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;

import com.example.testapplication.BR;
import com.example.testapplication.R;
import com.example.testapplication.databinding.FragRegistBinding;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class RegistFragment extends BaseFragment<FragRegistBinding, RegistFragmentViewModel> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.frag_regist;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initViewObservable() {
        viewModel.uc.pClickEvent.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                switch (integer) {
                    //返回
                    case 1:
                        NavHostFragment.findNavController(RegistFragment.this).popBackStack();
                        break;
                    //获取验证码
                    case 2:
                        if (viewModel.userName.get().length() != 11) {
                            binding.etRegisterPhone.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                            ToastUtils.showShort(R.string.common_phone_input_error);
                            return;
                        }

                        if (true) {
                            ToastUtils.showShort(R.string.common_code_send_hint);
                            binding.cvRegisterCountdown.start();
                            return;
                        }
                        break;
                    case 3:
                        if (viewModel.userName.get().length() != 11) {
                            binding.etRegisterPhone.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                            binding.btnRegisterCommit.showError(3000);
                            ToastUtils.showShort(R.string.common_phone_input_error);
                            return;
                        }

                        if (viewModel.code.get().length() != getResources().getInteger(R.integer.sms_code_length)) {
                            binding.etRegisterCode.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                            binding.btnRegisterCommit.showError(3000);
                            ToastUtils.showShort(R.string.common_code_error_hint);
                            return;
                        }

                        if(TextUtils.isEmpty(viewModel.password1.get())&& TextUtils.isEmpty(viewModel.password2.get())){
                            binding.etRegisterPassword1.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                            binding.etRegisterPassword2.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                            binding.btnRegisterCommit.showError(3000);
                            ToastUtils.showShort(R.string.common_password_input_error);
                            return;
                        }

                        if (!viewModel.password1.get().equals(viewModel.password2.get())) {
                            binding.etRegisterPassword1.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                            binding.etRegisterPassword2.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                            binding.btnRegisterCommit.showError(3000);
                            ToastUtils.showShort(R.string.common_password_input_unlike);
                            return;
                        }

                        // 隐藏软键盘
                        hideKeyboard(getActivity().getCurrentFocus());

                        if (true) {
                            binding.btnRegisterCommit.showProgress();
                            postDelayed(() -> {
                                binding.btnRegisterCommit.showSucceed();
                                postDelayed(() -> {
                                    NavHostFragment.findNavController(RegistFragment.this).popBackStack();
                                }, 1000);
                            }, 2000);
                            return;
                        }
                        break;

                }

            }
        });
    }
}
